package top.zhimingw.service.impl;

import org.springframework.stereotype.Service;
import top.zhimingw.pojo.Link;
import top.zhimingw.redis.LinkKey;
import top.zhimingw.service.BaseService;
import top.zhimingw.service.LinkService;

import java.util.List;

/**
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/6/1 16:22
 */
@Service("linkService")
public class LinkServiceImpl extends BaseService implements LinkService {

    @Override
    public boolean insert(Link link) {
        if(linkMapper.insert(link)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteById(Integer id) {
        if(linkMapper.deleteById(id)==1){
            return true;
        }
        return false;
    }

    @Override
    public Link queryById(Integer id) {
        return linkMapper.queryById(id);
    }

    /**
    * @Description: 查询全部数据
    * @Param: []
    * @return: java.util.List<top.zhimingw.pojo.Link>
    * @Author: zhimingw
    * @Date: 2021/6/1
    */
    @Override
    public List<Link> queryAll() {
        // 获取缓存中的友情链接
        List<Link> links = null;
        if (redisService.exists(LinkKey.getIndex,"")) {
            links = redisService.getList(LinkKey.getIndex,"",Link.class);
        }else {
            // 获取并存入缓存
            links = linkMapper.queryAll();
            redisService.setList(LinkKey.getIndex,"",links);
        }
        return links;
    }

    @Override
    public List<Link> queryAll(Link link) {
        return linkMapper.queryAll(link);
    }

    @Override
    public boolean update(Link link) {
        if(linkMapper.update(link)==1){
            return true;
        }
        return false;
    }
}
